<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2024-03-19 17:55:31
 * @LastEditors: smallWhite
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /huizhou_website_admin/src/views/index/components/modal.vue
-->
<template>
  <el-dialog :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <el-form ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="110px"
      class="demo-ruleForm">

      <el-form-item label="排序"
        prop="sort">
        <el-input
          v-model="ruleForm.sort"
          placeholder="请输入序号" />
      </el-form-item>
      <el-form-item label="名称"
        prop="name">
        <el-input
          v-model="ruleForm.name"
          placeholder="请输入导航名称" />
      </el-form-item>
      <el-form-item label="路径"
        prop="path">
        <el-input
          v-model="ruleForm.path"
          placeholder="请输入导航路径" />
      </el-form-item>
    </el-form>
    <span slot="footer"
      class="dialog-footer">
      <el-button
        @click="resetForm('ruleForm')">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      url: '',
      ruleForm: {
        sort: '',
        name: '',
        path: ''
      },
      list: [],
      rules: {
        name: [{ required: true, message: '请选择栏目', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        path: [{ required: true, message: '请输入路径', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(page, data) {
      this.dialogVisible = true
      if (page === 'add') {
        this.title = '添加'
        this.list = data
      } else if (page === 'adds') {
        this.title = '添加二级目录'
        this.ruleForm.parentId = data.id
      } else {
        this.title = '编辑'
        this.ruleForm.id = data.id
        this.ruleForm.name = data.name
        this.ruleForm.sort = data.sort
        this.ruleForm.path = data.path

        this.ruleForm.parentId = data.parentId
      }
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.url = res.data.fileName
      this.url = res.data.previewUrl + '/' + res.data.fileName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },

    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.title !== '编辑') {
            REQUEST_LIST.REQUST_FCN('MENUADD', this.ruleForm, true).then(res => {
              if (res.status === 200) {
                this.$emit('ok')
                this.$message.success('添加成功！')
                this.handleClose()
              }
            })
          } else {
            REQUEST_LIST.REQUST_FCN('MENUEDIT', this.ruleForm, true).then(res => {
              if (res.status === 200) {
                this.$emit('ok')
                this.$message.success('编辑成功！')
                this.handleClose()
              }
            })
          }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
